﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using YJYpublicClass;
using System.ComponentModel;
using System.Threading;

namespace YJYpublicClass
{
   public class DPL_24W60_T4 : ILightControl
    {
        public bool Connection
        {
            get
            {
                return sport.IsOpen;
            }
        }
        public string Mes { get; set; }
        
        private SerialPort sport = new SerialPort();

        private object syncRoot = new object();
       // private static object lockobj = new object();

       // private static FJI_FSK_24_T4 instance = null;

        private DPL_Config cft = new DPL_Config();
        public ConfigBase cf {
            get { return cft; }
            set { cft = (DPL_Config)value; }
        }

        public void Close()
        {
            lock (syncRoot)
            {
                sport.Close();
            }
        }


        public void Connect()
        {
            lock (syncRoot)
            {
                try
                {
                    if (sport.IsOpen)
                    {
                        sport.Close();
                    }
                    sport.PortName = "COM" + cft.port;
                    sport.BaudRate = cft.baudRate;
                    sport.StopBits = cft.stopBits;
                    sport.DataBits = cft.dataBits;
                    sport.ReadTimeout = cft.timeout;
                    sport.WriteTimeout = cft.timeout;
                    sport.Open();
                    sport.ReadExisting();
                }
                catch (Exception ex)
                {
                    Mes = ex.ToString();
                }
            }
        }

       public Boolean initCHControl()
        {
           //先亮
            string mes = string.Empty;
            cft.ch1OPEN = "T";
            cft.ch2OPEN = "T";
            cft.ch3OPEN = "T";
            cft.ch4OPEN = "T";
            string comdstr = "S" + cft.ch1SelV + cft.ch1OPEN + cft.ch2SelV + cft.ch2OPEN + cft.ch3SelV + cft.ch3OPEN + cft.ch4SelV + cft.ch4OPEN + "C#";          
            SendCommand(comdstr, out mes);
            Thread.Sleep(500);
           //在灭
            cft.ch1OPEN = "F";
            cft.ch2OPEN = "F";
            cft.ch3OPEN = "F";
            cft.ch4OPEN = "F";
            comdstr = "S" + cft.ch1SelV + cft.ch1OPEN + cft.ch2SelV + cft.ch2OPEN + cft.ch3SelV + cft.ch3OPEN + cft.ch4SelV + cft.ch4OPEN + "C#";
            SendCommand(comdstr, out mes);

            if (mes != string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool CloseChx(int chNo)
        {
            string mes = string.Empty;
            string comdstr ="";          
            switch(chNo)
            {
                case 0:
                    cft.ch1OPEN = "F";
                    break;
                case 1:
                    cft.ch2OPEN = "F";
                    break;
                case 2:
                    cft.ch3OPEN = "F";
                    break;
                case 3:
                    cft.ch4OPEN = "F";
                    break;
            }
            comdstr = "S" + cft.ch1SelV + cft.ch1OPEN + cft.ch2SelV + cft.ch2OPEN + cft.ch3SelV + cft.ch3OPEN + cft.ch4SelV + cft.ch4OPEN + "C#";
            SendCommand(comdstr, out mes);

            if (mes != string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

       public bool CloseAll()
        {
            string mes = string.Empty;
            string comdstr = "";
            cft.ch1OPEN = "F";
            cft.ch2OPEN = "F";
            cft.ch3OPEN = "F";
            cft.ch4OPEN = "F";
            comdstr = "S" + cft.ch1SelV + cft.ch1OPEN + cft.ch2SelV + cft.ch2OPEN + cft.ch3SelV + cft.ch3OPEN + cft.ch4SelV + cft.ch4OPEN + "C#";
            SendCommand(comdstr, out mes);
            if (mes != string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public int[] GetValue()
        {
            throw new NotImplementedException();
        }

        public int GetValue(int chNo)
        {
            string mes = string.Empty;
            switch (chNo)
            {
                case 0:
                    mes = cft.ch1SelV.ToString();
                    break;
                case 1:
                    mes = cft.ch2SelV.ToString();
                    break;
                case 2:
                    mes = cft.ch3SelV.ToString();
                    break;
                case 3:
                    mes = cft.ch4SelV.ToString();
                    break;

            }
            return Convert.ToInt32(mes);
        }

        public bool IsExists()
        {
            if (!sport.IsOpen)
            {
                return false;
            }
            return true;
        }

        public bool OpenChx(int chNo)
        {
            //全部打开
            string mes=string.Empty;
            string comdstr = "";
            switch (chNo)
            {
                case 0:
                    cft.ch1OPEN = "T";
                    break;
                case 1:
                    cft.ch2OPEN = "T";
                    break;
                case 2:
                    cft.ch3OPEN = "T";
                    break;
                case 3:
                    cft.ch4OPEN = "T";
                    break;
            }
            comdstr = "S" + cft.ch1SelV + cft.ch1OPEN + cft.ch2SelV + cft.ch2OPEN + cft.ch3SelV + cft.ch3OPEN + cft.ch4SelV + cft.ch4OPEN + "C#";
            SendCommand(comdstr, out mes);
            if(mes!=string.Empty)
            {
                return true;
            }else
            {
                return false;
            }
        }

        public bool SetValue(int chNo, int val)
        {
            string mes = string.Empty;
            switch (chNo)
            {
                case 0:
                    cft.ch1SelV = val;
                    break;
                case 1:
                    cft.ch2SelV = val;
                    break;
                case 2:
                    cft.ch3SelV = val;
                    break;
                case 3:
                    cft.ch4SelV = val;
                    break;
            }
            string  comdstr = "S" + cft.ch1SelV + cft.ch1OPEN + cft.ch2SelV + cft.ch2OPEN + cft.ch3SelV + cft.ch3OPEN + cft.ch4SelV + cft.ch4OPEN + "C#";
            SendCommand(comdstr, out mes);
            if(mes!= string.Empty)
            {
                return true;
                
            }else
            {
                return false;
            }
        }

        public bool SetValues(int[] channelArray, int[] values)
        {
            throw new NotImplementedException();
        }

        private bool SendChar(char ch)
        {
            sport.Write(new char[1] { ch }, 0, 1);
            char rch = (char)sport.ReadChar();
            return ch == rch;
        }

        private bool SendCommand(string cmd, out string ReCmd)
        {
            lock (syncRoot)
            {
                sport.WriteLine(cmd);
                Thread.Sleep(10);
                int n = sport.BytesToRead;
                Byte[] BUF = new byte[n];
                sport.Read(BUF, 0, n);
                ReCmd = System.Text.Encoding.UTF8.GetString(BUF);
                return true;
            }
            
        }

        protected string ExecuteCommand(string cmd)
        {
            lock (syncRoot)
            {
                string Recmd = string.Empty;
                if (SendCommand(cmd, out Recmd))
                {
                    return Recmd.Trim().ToUpper();
                }
                return string.Empty;
            }
        }
    }
    public class DPL_Config : ConfigBase
    {
  
        [Category("串口参数"), Description("int类型"), DisplayName("COM口号"), ReadOnly(false)]
        public int port { get; set; }
        [Category("串口参数"), Description("int类型"), DisplayName("波特率"), ReadOnly(false)]
        public int baudRate { get; set; }
        [Category("串口参数"), Description("int类型"), DisplayName("传输超时"), ReadOnly(false)]
        public int timeout { get; set; }
        [Category("串口参数"), Description("int类型"), DisplayName("停止位"), ReadOnly(false)]
        public StopBits stopBits { get; set; }
        [Category("串口参数"), Description("int类型"), DisplayName("数据位"), ReadOnly(false)]
        public int dataBits { get; set; }

        [Category("光源控制"), Description("int类型"), DisplayName("通道1值"), ReadOnly(false)]
        public int ch1SelV
        {
            get; set;
        }
        [Category("光源控制"), Description("int类型"), DisplayName("通道2值"), ReadOnly(false)]
        public int ch2SelV
        {
            get;
            set;
        }
        [Category("光源控制"), Description("int类型"), DisplayName("通道3值"), ReadOnly(false)]
        public int ch3SelV
        {
            get;
            set;
        }
        [Category("光源控制"), Description("int类型"), DisplayName("通道4值"), ReadOnly(false)]
        public int ch4SelV
        {
            get;
            set;
        }

        [Category("光源控制"), Description("sting类型F-开T-关"), DisplayName("通道1值开关"), ReadOnly(true)]
        public string ch1OPEN
        {
            get;
            set;
        }
        [Category("光源控制"), Description("sting类型T-开F-关"), DisplayName("通道2值开关"), ReadOnly(true)]
        public string ch2OPEN
        {
            get;
            set;
        }
        [Category("光源控制"), Description("sting类型T-开F-关"), DisplayName("通道3值开关"), ReadOnly(true)]
        public string ch3OPEN
        {
            get;
            set;
        }
        [Category("光源控制"), Description("sting类型T-开F-关"), DisplayName("通道4值开关"), ReadOnly(true)]
        public string ch4OPEN
        {
            get;
            set;
        }

        public DPL_Config()
        {
            port = 1;
            baudRate = 19200;
            timeout = 2000;
            stopBits = StopBits.One;
            dataBits = 8;
            ch1SelV = 100;
            ch2SelV = 100;
            ch3SelV = 100;
            ch4SelV = 100;
            ch1OPEN = "F";
            ch2OPEN = "F";
            ch3OPEN = "F";
            ch4OPEN = "F";
            //记录4个通道值
        }


}


}
